import { _decorator, Component, find, Node } from 'cc';
import { ModuleBase } from './ModuleBase';
import { ModuleMgr } from './ModuleMgr';
import { Emitter } from '../FrameWork/Common/Emitter';
const { ccclass, property } = _decorator;

@ccclass('Level1')
export class Level1 extends Component {
    private _moduleMgr = ModuleMgr.instance;
    private _camera: Node = null;
    private _cameraMoveSpeed: number = 300;
    private _cameraIsMoving: boolean = false;
    private _cameraMoveReverse: boolean = true;
    start() {
        //获取相机节点
        this._camera = find("Canvas/Camera");
        //存储所有模块
        this.saveModule();
        //初始化左右模块
        this._moduleMgr.onInitModule();
    }
    //存储所有的模块
    saveModule() {
        let children = this.node.children;
        for (let child of children) {
            let comp = child.getComponent(ModuleBase);
            if (comp) {
                this._moduleMgr.addModule(child.name, comp);
            }
        }
    }
    update(deltaTime: number) {
        this._moduleMgr.onUpdateModule(deltaTime);
        this.moveCamera(deltaTime);
    }

    //移动相机
    moveCamera(dt: number) {
        let pos = this._camera.position.clone();
        if (this._cameraMoveReverse && pos.x >= 668) {
            this._cameraMoveReverse = false;
            this._cameraMoveSpeed = 0;
            this.scheduleOnce(() => {
                this._cameraMoveSpeed = -300;
            }, 1);
        }
        if (this._cameraIsMoving && pos.x <= 0) {
            //相机停止移动
            this._cameraMoveSpeed = 0;
            //僵尸开始移动
            // Emitter.instance.emit("AllZombieMove");
        }
        pos.x += this._cameraMoveSpeed * dt;
        this._camera.setPosition(pos);
        this._cameraIsMoving = true;
    }
}